package cn.edu.web.controller;

import cn.edu.web.pojo.Bike;
import cn.edu.web.service.BikeService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.GeoResults;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
public class BikeController {

	@Autowired
	private BikeService bikeService;

	@Autowired
	private HttpServletRequest request;

	@RequestMapping("/bike_list")
	public String list() {
		return "bike/list";
	}

	@RequestMapping("/bike_add")
	public String toAdd() {
		return "bike/add";
	}

	@RequestMapping("/bike_edit")
	public String toEdit() {
		return "bike/edit";
	}

	@PostMapping("/bike")
	@ResponseBody
	public String add(@RequestBody String bike) {
		System.out.println(bike);
		Bike b = JSONObject.parseObject(bike, Bike.class);
		bikeService.save(b);

		return "success";
	}

	@PostMapping("/bike2")
	@ResponseBody
	public String add2(String id, String type, String longitude, String latitude, String desc) {
		double location[] = new double[2];
		location[0] = Double.parseDouble(longitude);
		location[1] = Double.parseDouble(latitude);

		Bike b = new Bike();
		b.setId(Long.parseLong(id));
		b.setStatus(Integer.parseInt(type));
		b.setLocation(location);
		b.setDesc(desc);
		System.out.println(b);
		bikeService.save(b);

		return "success";
	}

	@PostMapping("/bike_edit")
	@ResponseBody
	public String edit(String type, String desc) {
		String id = (String) request.getSession().getAttribute("id");
		Long bid = Long.parseLong(id);
		int status = Integer.parseInt(type);
//		System.out.println("session中获取的id:"+id);
//		System.out.printf("type 是 %s,   desc 是 %s",type,desc);

		bikeService.updateBikeStatus(bid, status, desc);
		return "单车状态修改成功";
	}

	@DeleteMapping("/bike/{ids}")
	@ResponseBody
	public String deleteByIds(@PathVariable("ids") Long[] ids) {
		bikeService.deleteByIds(ids);
		return "success";
	}

	@GetMapping("/bike/{id}")
	@ResponseBody
	public Bike getById(@PathVariable("id") Long id) {
		return bikeService.getById(id);
	}


	/**
	 * 查找当前坐标附近的单车
	 * @param longitude
	 * @param latitude
	 * @return
	 */
	@GetMapping("/bikes")
	@ResponseBody
	public GeoResults<Bike> findNear(double longitude, double latitude) {
		GeoResults<Bike> bikes = bikeService.findNear(longitude, latitude);
		return bikes;
	}

	/**
	 * 查找所有的的单车
	 * @return
	 */
	@GetMapping("/allbikes")
	@ResponseBody
	public List<Bike> findAll() {
		List<Bike> bikes = bikeService.findAll();
		return bikes;
	}

	/**
	 * 报修车辆
	 *
	 * @return
	 */
	@PostMapping("/bikeWarn")
	@ResponseBody
	// id的类型有问题.
	public String bikeWarn(String id, String types[], String desc) {
		StringBuffer problem = new StringBuffer();
		for (String type : types) {
			if (!type.isEmpty())
				problem.append(type + "   ");
		}
		problem.append(desc);
		String pro = problem.toString();
		int status = types.length;
		Long bid = Long.valueOf(id);
		bikeService.updateBikeStatus(bid, status, pro);
		return "success";
	}

	/**
	 *  查询单车最大的编号
	 */
	@GetMapping("/findMaxId")
	@ResponseBody
	// id的类型有问题.
	public long findMaxId() {
		long id = bikeService.findMaxId();
		return id;
	}

	/**
	 * 车辆开锁 修改状态
	 *
	 * @return
	 */
	@PostMapping("/unlock")
	@ResponseBody
	// id的类型有问题.
	public String unlock(String id, String status, String desc) {
		Long bid = Long.valueOf(id);
		int sta = Integer.valueOf(status);
		bikeService.updateBikeStatus(bid, sta, desc);
		System.out.println(id +",  "+ status+ ",  "+ desc);
		return "success";
	}

	@GetMapping("/saveId")
	public void saveId(String id) {
//		System.out.println(id);
		HttpSession session = request.getSession();
		session.setAttribute("id",id);
	}
}
